import CryptoJS from "crypto-js";
import { useRouter } from "vue-router";
import {userInfo} from '@/Api/api'
const key = CryptoJS.enc.Utf8.parse("ysh425ptjhcpo781"); //十六位十六进制数作为密钥偏移量
const iv = CryptoJS.enc.Utf8.parse("53c5a64c6be84e97"); //十六位十六进制数作为密钥偏移量
const token = localStorage.getItem("token");

export default () => {
    try {


        //解密
        function decrypt(text) {
            var result = CryptoJS.AES.decrypt(text, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7,
            });
            return result.toString(CryptoJS.enc.Utf8);
        }
        var jiami = ''


        if (!token) {
            const router = useRouter()
            router.push(-1)
            return
        }
        jiami = JSON.parse(decrypt(token));

        return jiami;

    } catch (error) {

    }
}

function encrypt(text) {
    return CryptoJS.AES.encrypt(text, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    }).toString();
}

export const setUser = (user) => {
    let token1 = JSON.stringify(user);

    localStorage.setItem("token", encrypt(token1));
}

export const getUser = async () => {
    const res = await userInfo()
    return res?.data?.data
}

